package com.briup.cms.service.impl;


import com.briup.cms.bean.Comment;
import com.briup.cms.dao.CommentMapper;

import com.briup.cms.dao.extend.CommentExtendMapper;
import com.briup.cms.exception.CustomerException;
import com.briup.cms.service.ICommentService;
import com.briup.cms.service.PageInfo;
import com.briup.cms.utils.MessageStatus;
import com.github.pagehelper.PageHelper;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CommentServiceImpl implements ICommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private com.briup.cms.dao.extend.CommentExtendMapper CommentExtendMapper;

    public PageInfo<Comment> pageQuery(int pageNum, int pageSize, String keywords) {
        PageHelper.startPage(pageNum, pageSize, true);
        List<Comment> list = this.CommentExtendMapper.pageQuery(keywords);
        return new PageInfo(list);
    }

    @Transactional
    public void deleteById(int id) throws CustomerException {
        this.commentMapper.deleteByPrimaryKey(Integer.valueOf(id));
    }

    @Transactional
    public void batchDelete(int[] ids) throws CustomerException {
        if (ids != null && ids.length > 0)
            for (int id : ids)
                this.commentMapper.deleteByPrimaryKey(Integer.valueOf(id));
    }

    @Transactional
    public void checkComment(int id, int status) throws CustomerException {
        Comment commentFromDB = this.commentMapper.selectByPrimaryKey(Integer.valueOf(id));
        if (commentFromDB == null)
            throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_COMMENT_NOT_EXIST);
        Comment comment = new Comment();
        comment.setCommentId(Integer.valueOf(id));
        comment.setCommentStatus(Integer.valueOf(status));
        this.commentMapper.updateByPrimaryKeySelective(comment);
    }
}
